package com.xiaoshuidi.cloud.module.finance.controller.app.finance;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.security.core.LoginUser;
import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.module.finance.controller.admin.finance.vo.account.FinanceAccountJournalPageReqVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.finance.vo.account.FinanceAccountJournalVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.finance.vo.account.FinanceAccountRefundReqVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.finance.vo.account.FinanceAccountVO;
import com.xiaoshuidi.cloud.module.finance.controller.app.finance.vo.*;
import com.xiaoshuidi.cloud.module.finance.convert.finance.FinanceConvert;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.finance.FinanceAccountDO;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.finance.FinanceAccountJournalExtDO;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.finance.FinanceAccountWithdrawDO;
import com.xiaoshuidi.cloud.module.finance.enums.finance.FinanceCustomerTypeEnum;
import com.xiaoshuidi.cloud.module.finance.enums.order.FinanceCommonStatusEnum;
import com.xiaoshuidi.cloud.module.finance.service.finance.FinanceAccountService;
import com.xiaoshuidi.cloud.module.finance.service.finance.FinanceAccountWithdrawService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.RoundingMode;
import java.util.List;

import static com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;

@Tag(name = "租客 APP - 卡包")
@Slf4j
@RestController
@RequestMapping("/finance/account")
public class AppFinanceAccountController {
    @Resource
    private FinanceAccountService financeAccountService;
    @Resource
    private FinanceAccountWithdrawService financeAccountWithdrawService;

    /**
     * 账户信息
     * @return
     */
    @GetMapping("/get")
    @Operation(summary = "账户信息")
    public CommonResult<FinanceAccountDO> detail() {
        LoginUser user = getLoginUser();
        FinanceAccountDO accountDO = financeAccountService.get(user.getId().toString(),
                FinanceCustomerTypeEnum.INDIVIDUAL.name());
        accountDO.setBalance(accountDO.getBalance().setScale(2, RoundingMode.DOWN));
        return CommonResult.success(accountDO);
    }

    /**
     * 账户信息
     * @return
     */
    @GetMapping("/get-all")
    @Operation(summary = "查询所有账户")
    public CommonResult<List<FinanceAccountVO>> allAccount() {
        LoginUser user = getLoginUser();
        List<FinanceAccountDO> accountDOs = financeAccountService.getAll(user);
        accountDOs.forEach(accountDO -> accountDO.setBalance(accountDO.getBalance().setScale(2, RoundingMode.DOWN)));
        return CommonResult.success(FinanceConvert.INSTANCE.convertToFinanceAcountVOList(accountDOs));
    }

    /**
     *
     * @param reqVO
     * @return
     */
    @Operation(summary = "钱包流水")
    @GetMapping("/journal/page")
    public CommonResult<PageResult<FinanceAccountJournalVO>> getPage(FinanceAccountJournalPageReqVO reqVO) {
        LoginUser user = getLoginUser();
        if (!StringUtils.hasText(reqVO.getCustType())) {
            reqVO.setCustId(user.getId().toString());
            reqVO.setCustType(FinanceCustomerTypeEnum.INDIVIDUAL.name());
        }
        reqVO.setStatus(FinanceCommonStatusEnum.SUCCESS.name());
        PageResult<FinanceAccountJournalExtDO> pageResult = financeAccountService.getJournalPage(reqVO);
        return CommonResult.success(FinanceConvert.INSTANCE.convertToFinanceAccountJournalPage(pageResult));
    }

    /**
     * 余额充值
     * 先保存一条流水, 充值成功后回调修改流水状态并添加余额
     * @param reqVO
     * @return
     */
    @Operation(summary = "余额充值")
    @PostMapping("/recharge")
    public CommonResult<Long> recharge(@RequestBody AppFinanceAccountRechargeReqVO reqVO) {
        LoginUser user = getLoginUser();
        Long journalId = financeAccountService.createJournal(user, reqVO);
        return CommonResult.success(journalId);
    }

    /**
     * 房屋账户提现
     * @param reqVO
     * @return
     */
    @PostMapping("/withdraw")
    @Operation(summary = "余额提现")
    public CommonResult<Void> withdraw(@Valid @RequestBody AppFinanceAccountWithdrawReqVO reqVO) {
        LoginUser user = getLoginUser();
        financeAccountWithdrawService.withdraw(user, reqVO);
        return CommonResult.success();
    }

    /**
     * 房屋账户提现申请列表
     * @return
     */
    @GetMapping("/withdraw/page")
    @Operation(summary = "余额提现申请")
    public CommonResult<PageResult<FinanceAccountWithdrawDO>> withdrawPage(AppFinanceAccountWithdrawPageReqVO reqVO) {
        LoginUser user = getLoginUser();
        PageResult<FinanceAccountWithdrawDO> page = financeAccountWithdrawService.withdrawPage(user, reqVO);
        return CommonResult.success(page);
    }

    /**
     * 余额提现同住人审批
     * @return
     */
    @PostMapping("/withdraw/review")
    @Operation(summary = "余额提现同住人审批")
    public CommonResult<Void> withdrawReview(@Valid @RequestBody AppFinanceAccountWithdrawReviewReqVO reqVO) {
        LoginUser user = getLoginUser();
        financeAccountWithdrawService.review(user, reqVO);
        return CommonResult.success();
    }

    /**
     * 个人余额提现
     *
     * @param reqVO
     * @return
     */
    @Operation(summary = "个人余额提现")
    @PostMapping("/withdrawal")
    public CommonResult<Void> withdrawal(@Valid @RequestBody AppFinanceAccountRefundReqVO reqVO) {
        financeAccountService.appWithdrawal(reqVO);
        return CommonResult.success();
    }

}
